﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Disco.Core
{
    public class VersionInfo
    {
        private readonly IList<VersionData> _versionData = new List<VersionData>();
        public IEnumerable<VersionData> VersionData
        {
            get { return new ReadOnlyCollection<VersionData>(_versionData); }
        }

        public VersionInfo() : this("*.*") { }

        internal VersionInfo(string assemblyPattern)
        {
            assemblyPattern = Path.ChangeExtension(assemblyPattern, "dll");

            var directory =
                Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", String.Empty);
            var assemblyNames = Directory.GetFiles(directory, assemblyPattern);
            foreach (var assembly in assemblyNames)
            {
                var assemblyName = Assembly.ReflectionOnlyLoadFrom(assembly).FullName;
                var assemblyVersion = FileVersionInfo.GetVersionInfo(assembly).FileVersion;
                var productName = FileVersionInfo.GetVersionInfo(assembly).ProductName;
                var productVersion = FileVersionInfo.GetVersionInfo(assembly).ProductVersion;

                _versionData.Add(new VersionData
                {
                    AssemblyName = assemblyName,
                    AssemblyVersion = assemblyVersion,
                    ProductName = productName,
                    ProductVersion = productVersion
                });
            }
        }
    }
}
